#include "ShaderProgram.h"
#include "Context.h"

#include "DrawAutomaticUniformFactoryCollection.h"
#include "LinkAutomaticUniformCollection.h"

#include "Device.h"

Renderer::ShaderProgram::ShaderProgram()
{
	_drawAutomaticUniforms = new std::vector<DrawAutomaticUniform*>();
}

Renderer::ShaderProgram::~ShaderProgram()
{
	//TODO ??
	delete _vertexAttributes;
	delete _uniforms;
	delete _drawAutomaticUniforms;
}

ShaderVertexAttributeCollection* Renderer::ShaderProgram::GetVertexAttributes()
{
	return _vertexAttributes;
}

UniformCollection* Renderer::ShaderProgram::GetUniforms()
{
	return _uniforms;
}

void Renderer::ShaderProgram::InitializeAutomaticUniforms(UniformCollection* uniforms)
{
	auto collection = uniforms->GetCollection();
	auto device = Device::GetInstance();
	for (auto iter = collection.begin(); iter != collection.end(); ++iter)
	{
		if (device->GetLinkAutomaticUniforms()->Contains(iter->first))
		{
			device->GetLinkAutomaticUniforms()->GetByName(iter->first)->Set(iter->second);
		}
		else if (device->GetDrawAutomaticUniformFactories()->Contains(iter->first))
		{
			_drawAutomaticUniforms->push_back(device->GetDrawAutomaticUniformFactories()->GetByName(iter->first)->Create(iter->second));
		}
			
	}
}

void Renderer::ShaderProgram::SetDrawAutomaticUniforms(Renderer::Context* conext, Renderer::DrawCommand* drawCommand, SceneState* sceneState)
{
	std::vector<Renderer::DrawAutomaticUniform*>::iterator itor = _drawAutomaticUniforms->begin();
	for (; itor != _drawAutomaticUniforms->end(); ++itor)
	{
		//_drawAutomaticUniforms[i].Set(context, drawState, sceneState);
		(*itor)->Set(conext, drawCommand, sceneState);
	}
}
